<template>
   <div>
      <!-- 搜索 -->
    <TopFilter
      ref="conditionFilter"
      :list="topFilterInit"
      :cols="3"
      labelWidth="100"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :isSelectColumn="isSelectColumn"
      :fetchapi="fetchapi" 
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
      columnsRef="projectOperating_projram_oemInquire_index_01"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-download" :loading="exportLoading" @click="exportData" >下载</el-button>
      </template>
    </FilterTable>
        <!-- 选择车型 -->
    <BaseDialog
      title="选择车型"
      :visible.sync="visibleDialog"
      width="700px"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <infoModal selectType="single" @close="selectModelBack" />
    </BaseDialog>
    <!-- 方案明细现象 -->
    <Drawer
      :visible.sync="visibleDrawer"
      title="方案明细"
      :width="920"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <detail v-model="initData" @close="detailClose" />
    </Drawer>
   </div>
</template>

<script>
import infoModal  from '@/pages/cms-common/model'
import detail from '@/pages/cms-pages/projectOperating/program/oemInquire/detail.vue'
import { dictionary } from '@/utils/dictMixin';
import {getOemProjectProgramList,exportOemProjectProgramList} from '@/api/cms-api/projectOperating/program/oemInquire';
import { Notification } from 'element-ui';
import { exportExcelFile,parseTime,formatdateByDay } from '@/utils';
import { type } from 'os';
export default {
  mixins: [dictionary],
  components: {
    infoModal,detail
  },
  data() {
    return {
      visibleDialog:false,
      visibleDrawer:false,
      isSelectColumn: false,
      topFilterInit: this.topFilterInitFun(),
      fetchapi: getOemProjectProgramList,
      initData:{},
      modelId:null,//查询条件车型ID
      columns: this.creatHeader(),
      fetchParams: {}, //ipa 接口
      exportLoading: false//导出按钮加载标识
    }
  },
  created(){
    this.initCondition();
  },
   mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    
  },
  //方法、事件
  methods: {
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT',  label: '方案编号', fieldName: 'programCode',  placeholder: '请输入', initialValue: ''},
        { type: 'INPUT',  label: '方案名称', fieldName: 'programName',  placeholder: '请输入', initialValue: ''},
        { type: 'SELECT', label: '方案状态', fieldName: 'programStatus', placeholder: '请选择', initialValue: '', itemList: this.createDictList('2528') },
        { type: 'RANGE_DATE',  label: '下发日期',  fieldName: 'issuedTime',  valueFormat: 'yyyy-MM-dd',   initialValue: '', placeholder: '请选择' },
        { type: 'INPUT',  label: '车型', fieldName: 'modelName',  placeholder: '请选择', initialValue: '', readonly:true,
             unitRender: () => {
                    return (
                      <div>
                        <el-button slot="append" icon="el-icon-search" onClick={() =>this.openModelDialog()}></el-button>
                        <el-button slot="append" icon="el-icon-delete" onClick={() => this.clearSelectModel()}></el-button>
                      </div>
                    );
              },
         },
        { type: 'INPUT',  label: '项目名称', fieldName: 'itemName',  placeholder: '请输入', initialValue: ''},
        { type: 'SELECT', label: '是否全网下发', fieldName: 'isNetworkIssued', placeholder: '请选择', initialValue: '', itemList: this.createDictList('1004') }
     ];
    },//搜索获取数据
    changeHandle(val) {
      if(!val.modelName || typeof(val.modelName)=='undefined' || val.modelName==''){
        this.modelId = '';
      }
      let extParams = {"modelId":this.modelId};
      if(val && val.issuedTime && typeof(val.issuedTime)!='undefinecd' && val.issuedTime.length>0){
        extParams.issuedTimeStart = val.issuedTime[0];
        extParams.issuedTimeEnd = val.issuedTime[1];
      }
      this.fetchParams=Object.assign({},val,extParams);
    },
     //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.drawerHandle(props.row,'show')}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        { title: '方案编号', dataIndex: 'programCode', sorter: true },
        { title: '方案状态', dataIndex: 'programStatus', sorter: true ,
           render: props => {    //编码转汉字
            return <span>{this.createDictText(props.row.programStatus, '2528')}</span>;
          }},
        { title: '方案名称', dataIndex: 'programName', sorter: true },
        { title: '项目周期', dataIndex: 'field6', dateFormat:'yyyy-MM-dd', sorter: true ,
          render: props => {    //编码转汉字
            return <span>{formatdateByDay(props.row.projectStartDate)}-{formatdateByDay(props.row.projectEndDate)}</span>;
          }},
        { title: '是否全网下发', dataIndex: 'isNetworkIssued', dateFormat:'yyyy-MM-dd', sorter: true ,
          render: props => {    //编码转汉字
            return <span>{this.createDictText(props.row.isNetworkIssued, '1004')}</span>;
          }},
        { title: '创建人', dataIndex: 'createdByName', sorter: true },
        { title: '创建时间', dataIndex: 'createdAt', dateFormat:'yyyy-MM-dd',  sorter: true  },
        { title: '下发人', dataIndex: 'issuedPersonName',  sorter: true  },
        { title: '下发时间', dataIndex: 'issuedTime', dateFormat:'yyyy-MM-dd', sorter: true }
      ];
    },
    openModelDialog() {
      this.visibleDialog = true;
    },
    //车型选择回填
    selectModelBack(data) {
      if(data && data.data && typeof(data.data)!=null){
        this.topFilterInit.find(item => item.fieldName === 'modelName').initialValue=data.data[0].modelName;//回显选择车型名称
        this.modelId = data.data[0].modelId;//存储车型ID
      }
      this.visibleDialog =false; //关闭弹窗
    },
    //清空选择车型
    clearSelectModel(){
        this.topFilterInit.find(item => item.fieldName === 'modelName').initialValue='';
        this.modelId = '';
    },
    //详情
    drawerHandle(row,type) {
      //查询功能明细页面不显示废除按钮
      let rowData = Object.assign({},row);
      rowData.isShowFeiChuBtn=false;
      //逻辑自己写
      this.initData =  {
        type: type,   //add/edit/show
        rowData: rowData,
      }; //行id
      this.visibleDrawer = true;
    },
    //关闭详情
    detailClose() {
      this.visibleDrawer = false;
    },
    //初始化查询条件
    initCondition(){
        let nDay = new Date();//当日
        let m3Day = new Date();//当前前三月日期
        m3Day.setMonth(m3Day.getMonth()-3);
        //初始化下发时间
        let _3monthDate = parseTime(m3Day,'{y}-{m}-{d} {h}:{i}:{s}');
        let currentDate = parseTime(nDay,'{y}-{m}-{d} {h}:{i}:{s}');
        this.topFilterInit.find(item => item.fieldName === 'issuedTime').initialValue = [_3monthDate,currentDate];
        this.fetchParams = Object.assign({},this.fetchParams,{"issuedTimeStart":_3monthDate,"issuedTimeEnd":currentDate});
    },
    async exportData(){
      let r = await this.$refs.conditionFilter.GET_FORM_DATA();
      let p = null;
      if(r!=null && r.length>1){
         p = r[1];
      }

      if(!p.modelName || typeof(p.modelName)=='undefined' || p.modelName==''){
        this.modelId = '';
      }
      let extParams = {"modelId":this.modelId};
      if(p && p.issuedTime && typeof(p.issuedTime)!='undefinecd' && p.issuedTime.length>0){
        extParams.issuedTimeStart = p.issuedTime[0];
        extParams.issuedTimeEnd = p.issuedTime[1];
      }

      let extP = Object.assign({},p,extParams);
      this.exportLoading = true
      exportOemProjectProgramList(extP).then(res => {
        exportExcelFile(res.data,'营销项目方案')
        this.exportLoading = false
      });
    },
     collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    }
  },
}
</script>

<style scoped>

</style>
